package cn.iocoder.yudao.module.biz.service.emergencyMaterials;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.biz.controller.admin.emergencyMaterials.vo.BizEmergencyMaterialsCreateReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.emergencyMaterials.vo.BizEmergencyMaterialsExportReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.emergencyMaterials.vo.BizEmergencyMaterialsPageReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.emergencyMaterials.vo.BizEmergencyMaterialsUpdateReqVO;
import cn.iocoder.yudao.module.biz.convert.emergencyMaterials.BizEmergencyMaterialsConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.emergencyMaterials.BizEmergencyMaterialsDO;
import cn.iocoder.yudao.module.biz.dal.mysql.emergencyMaterials.BizEmergencyMaterialsMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.EMERGENCY_MATERIALS_NOT_EXISTS;

/**
 * 应急物资 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BizEmergencyMaterialsServiceImpl implements BizEmergencyMaterialsService {

    @Resource
    private BizEmergencyMaterialsMapper emergencyMaterialsMapper;

    @Override
    public String createEmergencyMaterials(BizEmergencyMaterialsCreateReqVO createReqVO) {
        // 插入
        BizEmergencyMaterialsDO emergencyMaterials = BizEmergencyMaterialsConvert.INSTANCE.convert(createReqVO);
        emergencyMaterialsMapper.insert(emergencyMaterials);
        // 返回
        return emergencyMaterials.getId();
    }

    @Override
    public void updateEmergencyMaterials(BizEmergencyMaterialsUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateEmergencyMaterialsExists(updateReqVO.getId());
        // 更新
        BizEmergencyMaterialsDO updateObj = BizEmergencyMaterialsConvert.INSTANCE.convert(updateReqVO);
        emergencyMaterialsMapper.updateById(updateObj);
    }

    @Override
    public void deleteEmergencyMaterials(String id) {
        // 校验存在
        this.validateEmergencyMaterialsExists(id);
        // 删除
        emergencyMaterialsMapper.deleteById(id);
    }

    private void validateEmergencyMaterialsExists(String id) {
        if (emergencyMaterialsMapper.selectById(id) == null) {
            throw exception(EMERGENCY_MATERIALS_NOT_EXISTS);
        }
    }

    @Override
    public BizEmergencyMaterialsDO getEmergencyMaterials(String id) {
        return emergencyMaterialsMapper.selectById(id);
    }

    @Override
    public List<BizEmergencyMaterialsDO> getEmergencyMaterialsList(Collection<String> ids) {
        return emergencyMaterialsMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BizEmergencyMaterialsDO> getEmergencyMaterialsPage(BizEmergencyMaterialsPageReqVO pageReqVO) {
        return emergencyMaterialsMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BizEmergencyMaterialsDO> getEmergencyMaterialsList(BizEmergencyMaterialsExportReqVO exportReqVO) {
        return emergencyMaterialsMapper.selectList(exportReqVO);
    }

}
